/*
 * Author:      Marco Kuiper (http://www.marcofolio.net/)
 */
var colors;
var ratingInfobox;
var animationTime;
var ratedObjectType;
var emptyColors = function() {
	$(".rating li a").stop().animate({
		backgroundColor : "#333"
	}, animationTime);
};
// Function to colorize the right ratings
var colourizeRatings = function(nrOfRatings) {
	$(".rating li a").each(function() {
		if ($(this).parent().index() <= nrOfRatings) {
			$(this).stop().animate({
				backgroundColor : "#" + colours[nrOfRatings]
			}, animationTime);
		}
	});
};


$(document).ready(
		
		function() {
			
			
			// Variable to set the duration of the animation
	animationTime = 500;

			// Variable to store the colours
			colours = [ "bd2c33", "e49420", "ecdb00", "3bad54", "1b7db9" ];

			// Add rating information box after rating
			ratingInfobox = $("<div />").attr("class", "ratinginfo")
					.insertAfter($(".rating"));
			
			
			var id = $(".rating").parents()[1].id;
			
			if(id === "idea-rating"){
				ratedObjectType = "idea";
			}else if(id=== "topic-rating"){
				ratedObjectType ="topic";
			} else if(id === "plan-rating"){
				ratedObjectType ="plan";
			}
			oldRating = ideaId = document.getElementById(ratedObjectType+"Rating").value;
			oldRating = Math.round(oldRating) - 1;
			colourizeRatings(oldRating);
			
			// Handle the hover events
			$(".rating li a").hover(function() {
				$(".rating li a").stop().animate({
					backgroundColor : "#333"
				}, animationTime);
				// Empty the rating info box and fade in
				ratingInfobox.empty().stop().animate({
					opacity : 1
				}, animationTime);

				// Add the text to the rating info box
				$("<p />").html($(this).html()).appendTo(ratingInfobox);

				// Call the colourize function with the given index
				colourizeRatings($(this).parent().index());
			}, function() {

				// Fade out the rating information box
				ratingInfobox.stop().animate({
					opacity : 0
				}, animationTime);

				// Restore all the rating to their original colours
				$(".rating li a").stop().animate({
					backgroundColor : "#333"
				}, animationTime);
				if(newRating < 0){
					colourizeRatings(oldRating);
				}else{
					colourizeRatings(newRating);
				}
			});
			
			$(".rate").click(function(e){
				$(".rating").toggle();
			});
			
			// Prevent the click event and show the rating
			$(".rating li a").click(function(e) {
				// e.preventDefault();
				// here I put my code
				
				rating = ($(this).parent().index() + 1);
				

				ratedObjectId = document.getElementById(ratedObjectType+"Id").value;
				// ideaId = $("#ideaId").value;
				
				url = "/"+ratedObjectType+"/"+ratedObjectId+"/rating/"+rating;
				newRating = sendRating(url);
				emptyColors();
				

			});
		});

var newRating = -1;
function sendRating(url) {
	
	$.post(url, function(data) {
		newRating = data.charAt(0) - 1;
		$("#"+ ratedObjectType+"-rating label").html("Rating: " + (newRating + 1));
		colourizeRatings(newRating);
		// $('.rating li a').fadeIn(); // Show the suggestions box
		// $('#suggestions').html(data); // Fill the suggestions box
	});
	return newRating;
}
